<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      v-show="showSearch"
    >
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item label="" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="链接名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="" prop="type">
            <el-select v-model="queryParams.type" placeholder="链接类别" clearable style="width: 100%">
              <el-option
                v-for="dict in dict.type.sys_links_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
        <el-form-item label='' prop="createTime">
            <el-date-picker
              clearable
              style="width: 100%"
              v-model="createTime"
              type="daterange"
              value-format="yyyy-MM-dd HH:mm:ss"
              :default-time="['00:00:00','23:59:59']"
              range-separator="-"
              start-placeholder="创建开始日期"
              end-placeholder="创建结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
         
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          
        >删除</el-button>
      </el-col>
      <right-toolbar ref="table" :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- <el-table row-key="key" :key="isShow" v-loading="loading" :data="linksList" ref="linksTable"  @selection-change="handleSelectionChange">
      <template v-for="item in columns">
        <el-table-column v-if="item.visible && item.type" type="selection" :key="Math.random()" width="55"
                         align="center"
        ></el-table-column>
        <el-table-column :key="Math.random()" v-if="item.visible && !item.type" :class-name="item.className"
                         :prop="item.prop"
                         :label="item.label" :align="item.align" :type="item.type" :width="item.width"
        >
          <template slot-scope="scope">
            <template v-if="item.htmlType == 'imageUpload' && item.visible">
              <image-preview :src="scope.row[item.prop]" :width="50" :height="50"/>
            </template>

          <template v-else-if="item.htmlType == 'datetime' && item.visible">
            <span>{{ parseTime(scope.row[item.prop], '{y}-{m}-{d}') }}</span>
          </template>

          <template v-else-if="item.htmlType == 'checkbox' && item.visible" >
                <dict-tag v-if="item.htmlType == 'checkbox'" :options="dict.type[item.dictType]"
                          :value="scope.row[item.prop] ? scope.row[item.prop].split(',') : []"/>
                <dict-tag v-else :options="dict.type[item.dictType]" :value="scope.row[item.prop]"/>
          </template>
          <template v-else-if="item.htmlType =='operation'">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['newsCenter:links:edit']"
          >修改</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['newsCenter:links:remove']"
          >删除</el-button>
        </template>
          <template v-else>
            <span>{{ scope.row[scope.column.property] }}</span>
          </template>
          </template>
        </el-table-column>
      </template>

    </el-table>-->
    <el-table
      v-loading="loading"
      :data="linksList"
      ref="linksTable"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" width="55" align="center" />
      <el-table-column label="链接名称" align="left" prop="title" />
      <el-table-column label="链接类别" align="center" prop="type">
        <template slot-scope="scope">
            <dict-tag
              :options="dict.type.sys_links_type"
              :value="scope.row.type"
            />
          </template>
      </el-table-column>
      <el-table-column label="链接地址" align="center" prop="linkUrl" />
      <el-table-column label="创建日期" sortable align="center" prop="createTime" width="200" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdateStatus(scope.row)">发布</el-button> -->
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 新增或修改友情链接对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="35%" :close-on-click-modal="false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="链接名称" prop="title">
          <el-input v-model="form.title" clearable placeholder="请输入链接名称" />
        </el-form-item>
        <el-form-item label="链接类型" prop="type">
          <el-select
              v-model="form.type"
              placeholder="请选择链接类型"
              clearable
              style="width: 100%"
            >
              <el-option
                v-for="dict in dict.type.sys_links_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
        </el-form-item>
        <el-form-item label="链接地址" prop="linkUrl">
          <el-input type="textarea" :rows="3" v-model="form.linkUrl" placeholder="请输入链接地址" />
        </el-form-item>
        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">保 存</el-button>
        <el-button @click="cancel">返 回</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listLinks,
  getLinks,
  delLinks,
  addLinks,
  updateLinks
} from "@/api/information/links/links.js";
import { setStorageItem, getStorageItem } from "@/utils/storage.js";
import Sortable from "sortablejs";
export default {
  name: "Links",
  dicts:['sys_links_type'],
  data() {
    return {
      isShow: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      titles: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 友情链接表格数据
      linksList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 时间范围
      createTime:'',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        type: null,
      },
      // 列信息
      // columns: [
      //   {
      //     key: -1,
      //     label: "ID",
      //     visible: true,
      //     prop: "id",
      //     htmlType: "input",
      //     dictType: "",
      //     align: "center"
      //   },
      //   {
      //     key: 0,
      //     label: "链接名称",
      //     visible: true,
      //     prop: "title",
      //     htmlType: "input",
      //     dictType: "",
      //     align: "center"
      //   },
      //   {
      //     key: 1,
      //     label: "网址",
      //     visible: true,
      //     prop: "linkUrl",
      //     htmlType: "input",
      //     dictType: "",
      //     align: "center"
      //   },
      //   {
      //     key: 2,
      //     label: "链接类别：1-政府,2-事业单位,3-企业单位",
      //     visible: true,
      //     prop: "type",
      //     htmlType: "select",
      //     dictType: "",
      //     align: "center"
      //   },
      //   {
      //     key: 3,
      //     label: "顺序",
      //     visible: true,
      //     prop: "sequence",
      //     htmlType: "input",
      //     dictType: "",
      //     align: "center"
      //   },
      //   {
      //     key: 4,
      //     label: "创建者",
      //     visible: true,
      //     prop: "createBy",
      //     htmlType: "input",
      //     dictType: "",
      //     align: "center"
      //   },
      //   {
      //     key: 5,
      //     label: "创建时间",
      //     visible: true,
      //     prop: "createTime",
      //     htmlType: "datetime",
      //     dictType: "",
      //     align: "center"
      //   },
      //   {
      //     key: 6,
      //     label: "更新者",
      //     visible: true,
      //     prop: "updateBy",
      //     htmlType: "input",
      //     dictType: "",
      //     align: "center"
      //   },
      //   {
      //     key: 7,
      //     label: "更新时间",
      //     visible: true,
      //     prop: "updateTime",
      //     htmlType: "datetime",
      //     dictType: "",
      //     align: "center"
      //   },
      //   {
      //     key: 8,
      //     label: "备注",
      //     visible: true,
      //     prop: "remark",
      //     htmlType: "textarea",
      //     dictType: "",
      //     align: "center"
      //   },
      //   {
      //     key: 9,
      //     label: "租户ID",
      //     visible: true,
      //     prop: "tenantId",
      //     htmlType: "input",
      //     dictType: "",
      //     align: "center"
      //   },
      //   {
      //     key: 0,
      //     label: `操作`,
      //     visible: true,
      //     prop: "operation",
      //     htmlType: "operation",
      //     align: "center",
      //     width: "200",
      //     className: "fixed-width"
      //   }
      // ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "链接名称不能为空", trigger: "blur" }
        ],
        linkUrl: [{ required: true, message: "链接地址不能为空", trigger: "blur" }],
        type: [
          {
            required: true,
            message: "链接类别不能为空",
            trigger: "change"
          }
        ]
      }
    };
  },
  created() {
    // let data = getStorageItem(this.$options.name);
    // if (data) {
    //   this.columns = data;
    // }
    this.getList();
  },
  mounted() {
    // this.columnDrop();
  },
  methods: {
    //列拖拽
    // columnDrop() {
    //   const wrapperTr = this.$refs.linksTable.$el.querySelector(
    //     ".el-table__header-wrapper tr"
    //   );
    //   this.sortable = Sortable.create(wrapperTr, {
    //     animation: 180,
    //     delay: 30,
    //     // draggable: ".drag",
    //     onEnd: evt => {
    //       if (evt.oldIndex === evt.newIndex) return;
    //       const oldItem = this.columns[evt.oldIndex];
    //       const newItem = this.columns[evt.newIndex];
    //       this.$set(this.columns, evt.newIndex, oldItem);
    //       this.$set(this.columns, evt.oldIndex, newItem);
    //       setStorageItem(this.$options.name, this.columns);
    //     }
    //   });
    // },
    // dataChange(data) {
    //   for (let item in this.columns) {
    //     const key = this.columns[item].key;
    //     this.columns[item].visible = !data.includes(key);
    //   }

    //   setStorageItem(this.$options.name, this.columns);
    // },
    // changeColumns(data) {
    //   this.columns = data;
    //   setStorageItem(this.$options.name, data);
    //   this.isShow = !this.isShow;
    // },
    /** 查询友情链接列表 */
    getList() {
      this.loading = true;
      listLinks(this.addDateRange(this.queryParams,this.createTime) ).then(response => {
        this.linksList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        linkUrl: null,
        type: null,
        sequence: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        tenantId: null
      };
      this.resetFormData("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.createTime = []
      this.resetFormData("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.titles = selection.map(item => item.title);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增友情链接";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getLinks(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改友情链接";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLinks(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLinks(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const titles = row.title || this.titles;
      this.$modal
        .confirm('是否确认删除文件标题为"' + titles + '"的数据项？')
        .then(function() {
          return delLinks(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "newsCenter/links/export",
        {
          ...this.queryParams
        },
        `links_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>
